﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Resources;
using System.Globalization;
using LyncServerExtension.PluginModel;
using LyncServerExtension.Plugin.Snom.Resources;

namespace LyncServerExtension.Plugin.Snom
{
    /// <summary>
    /// Class which implements a SNOM IP Phone result builder.
    /// </summary>
    public sealed class SnomIPPhoneResultBuilder : ILyncResultBuilderPlugin
    {
        private ResourceManager resourceManager;

        /// <summary>
        /// Gets the plugin id.
        /// </summary>
        public string Id
        {
            get { return "SnomIPPhone"; }
        }

        /// <summary>
        /// Gets the plugin version.
        /// </summary>
        public string Version
        {
            get { return "1.1"; }
        }

        /// <summary>
        /// Gets or sets the culture.
        /// </summary>
        /// <value>
        /// The culture to use to internationalize results of this builder.
        /// </value>
        public CultureInfo Culture { get; set; }

        /// <summary>
        /// Called when [load].
        /// </summary>
        /// <param name="context">The context.</param>
        public void OnLoad(IPluginContext context)
        {
            resourceManager = new ResourceManager(typeof(Messages));
        }

        /// <summary>
        /// Called when [unload].
        /// </summary>
        public void OnUnload()
        {
        }

        /// <summary>
        /// Sets the authorized owners.
        /// </summary>
        /// <param name="users">The users.</param>
        /// <returns>
        /// A result for the operation.
        /// </returns>
        public ILyncResult SetAuthorizedOwners(IEnumerable<LyncUser> users)
        {
            return new SnomIPPhoneText
            {
                Title = GetMessage("CallForwardingDelegationTitle"),
                Prompt = string.Empty,
                Text = GetMessage("AuthorizedOwnerList", string.Join(", ", users.Select(u => u.Sip)))
            };
        }

        /// <summary>
        /// Sets the authorized delegates.
        /// </summary>
        /// <param name="ownerSip">The owner sip.</param>
        /// <param name="users">The users.</param>
        /// <returns>
        /// A result for the operation.
        /// </returns>
        public ILyncResult SetAuthorizedDelegates(string ownerSip, IEnumerable<LyncUser> users)
        {
            return new SnomIPPhoneText
            {
                Title = GetMessage("CallForwardingDelegationTitle"),
                Prompt = string.Empty,
                Text = GetMessage("AuthorizedDelegateList", ownerSip, string.Join(", ", users.Select(u => u.Sip)))
            };
        }

        /// <summary>
        /// Sets the delegates.
        /// </summary>
        /// <param name="ownerSip">The owner sip.</param>
        /// <param name="users">The users.</param>
        /// <returns>
        /// A result for the operation.
        /// </returns>
        public ILyncResult SetDelegates(string ownerSip, IEnumerable<LyncUser> users)
        {
            return new SnomIPPhoneText
            {
                Title = GetMessage("CallForwardingDelegationTitle"),
                Prompt = string.Empty,
                Text = GetMessage("DelegateList", ownerSip, string.Join(", ", users.Select(u => u.Sip)))
            };
        }

        /// <summary>
        /// Sets the call forwarding delegation.
        /// </summary>
        /// <param name="param">The param.</param>
        /// <param name="state">A non used value which can be used by plugins for customisation.
        /// Can be used by a plugin to save the current enabled/disabled state for example.</param>
        /// <returns>
        /// A result for the operation.
        /// </returns>
        public ILyncResult SetCallForwardingDelegation(LyncCallForwardingParameter param, string state)
        {
            int ledNumber;
            int.TryParse(state, out ledNumber);

            return new SnomIPPhoneText
            {
                Title = GetMessage("CallForwardingDelegationTitle"),
                Prompt = string.Empty,
                Text = param.IsEnabled
                    ? GetMessage("CallForwardingDelegationEnabledText", param.DelegateSip, param.OwnerSip)
                    : GetMessage("CallForwardingDelegationDisabledText", param.DelegateSip, param.OwnerSip),
                Led = new SnomIPPhoneTextLed
                {
                    Number = ledNumber,
                    IsEnabled = param.IsEnabled
                }
            };
        }

        /// <summary>
        /// Errors the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <returns>
        /// A result for the operation.
        /// </returns>
        public ILyncResult Error(string message)
        {
            return new SnomIPPhoneText
            {
                Title = GetMessage("CallForwardingDelegationTitle"),
                Prompt = string.Empty,
                Text = string.Format("Error: {0}", message),
                Led = null
            };
        }

        /// <summary>
        /// Gets an internationalized message given its key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="args">The args.</param>
        /// <returns></returns>
        private string GetMessage(string key, params object[] args)
        {
            return (args != null)
                ? String.Format(resourceManager.GetString(key, Culture), args)
                : resourceManager.GetString(key, Culture);
        }
    }
}
